package com.fingard.net;

import java.io.IOException;
import java.util.ArrayList;

import com.fingard.dsp.bank.directConfig.FtpSet;
import com.fingard.generalobj.ExecResponse;
import com.jcraft.jsch.SftpException;

/**根据FtpSet.ftpType判断是sftp或ftp*/
public class MixFtp {
	
	private FtpPortClient ftpClient = null;
	private SftpPortClient sftp = null;
	public FtpSet ftpSet;
	
	public MixFtp(FtpSet pFtpSet) {
        ftpSet = pFtpSet;
        if("sftp".equalsIgnoreCase(ftpSet.ftpType)){
        	sftp = new SftpPortClient(ftpSet);
        }else{
        	ftpClient = new FtpPortClient(ftpSet);
        }
    }
	
	/**连接*/
	public void connect() throws IOException {
		if(sftp != null){
			sftp.connect();
		}else{
			ftpClient.connect();
		}
	}
	
	/**断开*/
	public void disconnect() throws IOException {
		if(sftp != null){
			sftp.disconnect();
		}else{
			ftpClient.disconnect();
		}
	}
	
	/**是否存在文件*/
	public boolean existFile(String pFileName) throws SftpException{
		if(sftp != null){
			return sftp.existFile(pFileName);
		}else{
			return ftpClient.existFile(pFileName);
		}
	}
	
	/**上传本地文件到指定目录，如果目录不存在则先创建目录*/
	public FtpReceiver uploadFileToDir(String pLocalFile, String pUpDir, String pTrgFileName) throws IOException {
		if(sftp != null){
			return sftp.uploadFileToDir(pLocalFile, pUpDir, pTrgFileName);
		}else{
			return ftpClient.uploadFileToDir(pLocalFile, pUpDir, pTrgFileName);
		}
	}
	
	/**创建目录（先判断目录是否已存在，如不存在则创建）*/
	public ExecResponse makeDirIfNotExist(String remotePath) throws SftpException, Exception {
		if(sftp != null){
			return sftp.makeDirIfNotExist(remotePath);
		}else{
			return ftpClient.makeDirIfNotExist(remotePath);
		}
	}
	
	/**取得目录下的所有文件名或子目录名*/
    public ArrayList<String> listNames(String directory) throws Exception {
		if(sftp != null){
			return sftp.listNames(directory);
		}else{
			return ftpClient.listNames(directory);
		}
	}
    
    /**从指定目录下载文件*/
    public FtpReceiver downloadFileFromDir(String pDownDir, String pDownFileName, String pSaveLocalFile) throws IOException {
    	if(sftp != null){
    		return sftp.downloadFileFromDir(pDownDir, pDownFileName, pSaveLocalFile);
    	}else{
    		return ftpClient.downloadFileFromDir(pDownDir, pDownFileName, pSaveLocalFile);
    	}
    }
}
